<?php 
	/**
	* 
	*/
	namespace Admin\Model;
 
	class UserModel extends   CommonModel
	{
        public $_validate	=	array(
            // array('account','/^[a-z]\w(.|\n){3,}$/i','帐号格式错误'),
         //   array('account','/^{3,15}$/','用户名不符合要求，长度为3-15个字节。'),
            array('password','require','密码必须'),
            array('nickname','require','昵称必须'),
            array('repassword','require','确认密码必须'),
            array('repassword','password','确认密码不一致',self::EXISTS_VALIDATE,'confirm'),
            array('account','','帐号已经存在',self::EXISTS_VALIDATE,'unique',self::MODEL_INSERT),
          //  array('email','checkEmail','邮箱已经存在。',1,'callback'),
        );

        public $_auto		=	array(
            array('password','pwdHash',self::MODEL_BOTH,'callback'),
            array('create_time','time',self::MODEL_INSERT,'function'),
            array('update_time','time',self::MODEL_UPDATE,'function'),

        );

        protected function pwdHash() {
            if(isset($_POST['password'])) {
                return pwdHash($_POST['password']);
            }else{
                return false;
            }
        }

        function hasRole($userId) {
            $table = $this->tablePrefix.'role_user';
            $rs = $this->db->query('select role_id from '.$table.' where user_id='.$userId);
            if (isset($rs)) {
                return true;
            }
            return false;
        }

        // checkEmail方法
        protected function checkEmail(){
            $user = D('User');
        // 新用户注册，验证唯一
            if(empty($_POST['uid'])){
                if($user->getByEmail($_POST['email'])){
                    return false;
                }else{
                    return true;
                }
            }else{
                // 更改资料判断邮箱与其他人的邮箱是否相同
                if($user->where("uid!={$_POST['uid']} and email='{$_POST['email']}'")->find()){
                    return false;
                }else{
                    return true;
                }
            }
        }

        /**
         * 是否存在邮件
         * @param $email
         * @return bool
         */
        public function hasEmail($email) {
            $model = M('email_list');
            $map['email'] = $email;
            $map['is_main'] = 1;
            if ($model->where($map)->find()){
                return true;
            }
            return false;
        }

        /**
         * 通过用户名修改邮箱地址
         * @param $name
         * @param $suffix
         */
        public function changeUserEmailByName($name,$suffix) {

            $model = D('email_list');

            $data1 = $this->field('id,email')->where('email like "'.strtolower($name).'%"')->find();
            $data2 = $model->field('id,email')->where('email like "'.strtolower($name).'%"')->find();
            $value['email'] = $name.$suffix;
            if ($data1) {
                if ($this->where('id=' . $data1['id'])->save($value) === false) {
                    echo $this->getError();
                    die;
                }
            }
            if ($data2) {
                if ( $model->where('id=' . $data2['id'])->save($value) === false) {
                    echo $model->getLastSql();
                    die;
                }
            }


        }


	}